//
//  WKGiftView.m
//  WKMaybell
//
//  Created by 何文虎 on 2019/4/1.
//  Copyright © 2019 何文虎. All rights reserved.
//

#import "WKGiftView.h"
#define CONTENTHEIGHT 204.f

@interface WKGiftView ()<UITextFieldDelegate>
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)UILabel *titleLb;
@property(nonatomic,strong)UIButton *surplusBtn;
@property(nonatomic,strong)UIButton *pay_5Btn;
@property(nonatomic,strong)UIButton *pay_10Btn;
@property(nonatomic,strong)UIButton *pay_20Btn;
@property(nonatomic,strong)UIButton *pay_50Btn;
@property(nonatomic,strong)UITextField *pay_text;
@property(nonatomic,strong)UIButton *senderBtn;
@end

@implementation WKGiftView

+ (void)showWKGiftView{
    WKGiftView *view = [[WKGiftView alloc]init];
    [view show];
    
}

- (instancetype)init{
    if (self == [super init]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
//    self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.contentView  = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth-24, CONTENTHEIGHT)];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.layer.masksToBounds = YES;
    _contentView.layer.cornerRadius = 5;
    [self addSubview:self.contentView];
    
    [_contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(24);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(14);
    }];
    
    [_contentView addSubview:self.surplusBtn];
    [self.surplusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.right.mas_equalTo(-24);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(14);
    }];

    CGFloat lable_width = (ScreenWidth - 48 -24)/2;

    [_contentView addSubview:self.pay_5Btn];
    [self.pay_5Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(49);
        make.left.mas_equalTo(24);
        make.width.mas_equalTo(lable_width);
        make.height.mas_equalTo(35);
    }];

    [_contentView addSubview:self.pay_10Btn];
    [self.pay_10Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(49);
        make.right.mas_equalTo(-24);
        make.width.mas_equalTo(lable_width);
        make.height.mas_equalTo(35);
    }];

    [_contentView addSubview:self.pay_20Btn];
    [self.pay_20Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pay_5Btn.mas_bottom).offset(15);
        make.left.mas_equalTo(self.pay_5Btn.mas_left);
        make.width.mas_equalTo(lable_width);
        make.height.mas_equalTo(35);
    }];

    [_contentView addSubview:self.pay_50Btn];
    [self.pay_50Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pay_20Btn.mas_top);
        make.right.mas_equalTo(-24);
        make.width.mas_equalTo(lable_width);
        make.height.mas_equalTo(35);
    }];

    [_contentView addSubview:self.senderBtn];
    [self.senderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pay_50Btn.mas_bottom).offset(15);
        make.right.mas_equalTo(-24);
        make.width.mas_equalTo(71);
        make.height.mas_equalTo(35);
    }];

    [_contentView addSubview:self.pay_text];
    [self.pay_text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.senderBtn.mas_top);
        make.right.mas_equalTo(self.senderBtn.mas_left).offset(-15);
        make.left.mas_equalTo(24);
        make.height.mas_equalTo(35);
    }];
    self.pay_text.delegate = self;
    
    _titleLb.text = @"为 某某 打call~";
    [_surplusBtn setTitle:@"剩余痘币: 50" forState:UIControlStateNormal];
    [_pay_5Btn setTitle:@"5 痘币" forState:UIControlStateNormal];
    [_pay_10Btn setTitle:@"10 痘币" forState:UIControlStateNormal];
    [_pay_20Btn setTitle:@"20 痘币" forState:UIControlStateNormal];
    [_pay_50Btn setTitle:@"50 痘币" forState:UIControlStateNormal];
   
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)show{
    

    [self addSubview:_contentView];
    _contentView.center = self.center;
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.backgroundColor = [UIColor clearColor];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [keyWindow addSubview:self];
    NSArray *view_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    NSArray *view_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    [keyWindow addConstraints:view_H];
    [keyWindow addConstraints:view_V];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden)];
    [self addGestureRecognizer:tap];
    
    _contentView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CONTENTHEIGHT);
    [UIView animateWithDuration:0.1f animations:^{
        _contentView.frame = CGRectMake(0, ScreenHeight-CONTENTHEIGHT-HS_TabbarSafeBottomMargin, ScreenWidth, CONTENTHEIGHT);
    }];
    
}

- (void)hidden{
    
    [_pay_text resignFirstResponder];
    
    [UIView animateWithDuration:0.2f animations:^{
        _contentView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CONTENTHEIGHT);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
    
}



#pragma -mark UITextField Delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
}

// 点击非TextField区域取消第一响应者
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
     [_pay_text resignFirstResponder];
}

// 点击键盘Return键取消第一响应者
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
   [_pay_text resignFirstResponder];
     return  YES;
 }

// 键盘监听事件
- (void)keyboardAction:(NSNotification*)sender{
    // 通过通知对象获取键盘frame: [value CGRectValue]
      NSDictionary *useInfo = [sender userInfo];
     NSValue *value = [useInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
     CGFloat keyboardHeight = [value CGRectValue].size.height;
  
      // <注意>具有约束的控件通过改变约束值进行frame的改变处理
    if ([sender.name isEqualToString:UIKeyboardWillShowNotification]) {
        _contentView.frame = CGRectMake(0, ScreenHeight-CONTENTHEIGHT-HS_TabbarSafeBottomMargin-keyboardHeight, ScreenWidth, CONTENTHEIGHT);
    }else{
        _contentView.frame = CGRectMake(0, ScreenHeight-CONTENTHEIGHT-HS_TabbarSafeBottomMargin, ScreenWidth, CONTENTHEIGHT);
    }
}





- (UILabel*)titleLb{
    if (!_titleLb) {
        _titleLb = [[UILabel alloc]init];
    }
    return _titleLb;
}

- (UIButton*)surplusBtn{
    if (!_surplusBtn) {
        _surplusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_surplusBtn setTitleColor:UIColorFromHex(0x3C3C3C) forState:UIControlStateNormal];
        _surplusBtn.titleLabel.font = PFSC_MediumFont(12);
    
    }
    return _surplusBtn;
}

- (UIButton*)pay_5Btn{
    if (!_pay_5Btn) {
        _pay_5Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pay_5Btn setTitleColor:UIColorFromHex(0x3C3C3C) forState:UIControlStateNormal];
        _pay_5Btn.titleLabel.font = PFSC_MediumFont(15);
        _pay_5Btn.layer.borderWidth = 1;
        _pay_5Btn.layer.borderColor = UIColorFromHex(0x979797).CGColor;
        _pay_5Btn.layer.masksToBounds = YES;
        _pay_5Btn.layer.cornerRadius = 5;

    }
    return _pay_5Btn;
}

- (UIButton*)pay_10Btn{
    if (!_pay_10Btn) {
        _pay_10Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pay_10Btn setTitleColor:UIColorFromHex(0x3C3C3C) forState:UIControlStateNormal];
        _pay_10Btn.titleLabel.font = PFSC_MediumFont(15);
        _pay_10Btn.layer.borderWidth = 1;
        _pay_10Btn.layer.borderColor = UIColorFromHex(0x979797).CGColor;
        _pay_10Btn.layer.masksToBounds = YES;
        _pay_10Btn.layer.cornerRadius = 5;
    }
    return _pay_10Btn;
}

- (UIButton*)pay_20Btn{
    if (!_pay_20Btn) {
        _pay_20Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pay_20Btn setTitleColor:UIColorFromHex(0x3C3C3C) forState:UIControlStateNormal];
        _pay_20Btn.titleLabel.font = PFSC_MediumFont(15);
        _pay_20Btn.layer.borderWidth = 1;
        _pay_20Btn.layer.borderColor = UIColorFromHex(0x979797).CGColor;
        _pay_20Btn.layer.masksToBounds = YES;
        _pay_20Btn.layer.cornerRadius = 5;
    }
    return _pay_20Btn;
}

- (UIButton*)pay_50Btn{
    if (!_pay_50Btn) {
        _pay_50Btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pay_50Btn setTitleColor:UIColorFromHex(0x3C3C3C) forState:UIControlStateNormal];
        _pay_50Btn.titleLabel.font = PFSC_MediumFont(15);
        _pay_50Btn.layer.borderWidth = 1;
        _pay_50Btn.layer.borderColor = UIColorFromHex(0x979797).CGColor;
        _pay_50Btn.layer.masksToBounds = YES;
        _pay_50Btn.layer.cornerRadius = 5;
    }
    return _pay_50Btn;
}

- (UITextField*)pay_text{
    if (!_pay_text) {
        _pay_text = [[UITextField alloc]init];
        _pay_text.placeholder = @" 自定义痘币数量~";
        _pay_text.font = PFSC_MediumFont(14);
        _pay_text.layer.borderWidth = 1;
        _pay_text.layer.borderColor = UIColorFromHex(0x979797).CGColor;
        _pay_text.layer.masksToBounds = YES;
        _pay_text.layer.cornerRadius = 5;
        _pay_text.keyboardType = UIKeyboardTypeNumberPad;
        _pay_text.returnKeyType = UIReturnKeyDone;
    }
    return _pay_text;
}

- (UIButton*)senderBtn{
    if (!_senderBtn) {
        _senderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_senderBtn setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateNormal];
        _senderBtn.backgroundColor = BACK_COLOR;
        _senderBtn.titleLabel.font = PFSC_MediumFont(15);
        [_senderBtn setTitle:@"送出" forState:UIControlStateNormal];
        _senderBtn.layer.masksToBounds = YES;
        _senderBtn.layer.cornerRadius = 5;
    }
    return _senderBtn;
}

@end
